#!/usr/bin/env python

import argparse
import logging
import json
import os
import shutil
import struct
import subprocess
import sys


def get_platform():
    if sys.platform == "linux":
        return "manylinux_%s" % os.uname().machine
    elif sys.platform == "darwin":
        return "macosx_%s" % os.uname().machine
    elif sys.platform == "win32":
        return "win%s" % (struct.calcsize("P") * 8)
    else:
        raise Exception("Unsupported platfom %s" % sys.platform)


parser = argparse.ArgumentParser(description="Fetch and extract tarballs")
parser.add_argument("destination_dir")
parser.add_argument("--cache-dir", default="tarballs")
parser.add_argument("--config-file", default=__file__ + ".json")
args = parser.parse_args()
logging.basicConfig(level=logging.INFO)

# read config file
with open(args.config_file, "r") as fp:
    config = json.load(fp)

# create fresh destination directory
logging.info("Creating directory %s" % args.destination_dir)
if os.path.exists(args.destination_dir):
    shutil.rmtree(args.destination_dir)
os.mkdir(args.destination_dir)

for url_template in config["urls"]:
    tarball_url = url_template.replace("{platform}", get_platform())

    # download tarball
    tarball_name = tarball_url.split("/")[-1]
    tarball_file = os.path.join(args.cache_dir, tarball_name)
    if not os.path.exists(tarball_file):
        logging.info("Downloading %s" % tarball_url)
        if not os.path.exists(args.cache_dir):
            os.mkdir(args.cache_dir)
        subprocess.check_call(
            ["curl", "--location", "--output", tarball_file, "--silent", tarball_url]
        )

    # extract tarball
    logging.info("Extracting %s" % tarball_name)
    subprocess.check_call(["tar", "-C", args.destination_dir, "-xf", tarball_file])
